package com.lhy.backend.controller;


import com.lhy.backend.common.response.ResResult;
import com.lhy.backend.domain.dto.LoginDto;
import com.lhy.backend.domain.entity.user.SysMenu;
import com.lhy.backend.domain.entity.user.SysUser;
import com.lhy.backend.domain.vo.RouterVO;
import com.lhy.backend.serivice.IAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.awt.desktop.SystemEventListener;
import java.util.List;

@RestController
@RequestMapping("auth")
public class AuthController {

    @Autowired
    private IAuthService authService;

    @PostMapping("login")
    public ResResult login(@RequestBody LoginDto loginDto) {
        String login = authService.login(loginDto);
        if ("error".equals(login)) {
            return ResResult.error("登录失败");
        } else {
            return ResResult.success(login);
        }
    }

    @GetMapping("getUserInfo")
    public ResResult getUserInfo() {
        SysUser sysUser = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return ResResult.success(sysUser);
    }

    @GetMapping("getMenus")
    public ResResult getMenus() {
        List<RouterVO> menus = authService.getMenus();
        //获取当前登录的用户id
        return ResResult.success(menus);
    }

}
